<?php
namespace Home\Controller;

class NewsPushController extends ParentController {

     protected function _initialize() {
        if (!in_array(ACTION_NAME, array('getNewsList'))) {
            parent::_initialize();
            $this->switchTimeUtc();
        }

        $this->validateUtil = new \Org\Util\Validate();
    }
	/**
     * 消息推送控制器
     * @modify wenhui 2017-03-16    创建
     */
	public function getNewsList() {
		$param = I('post.');

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $hotelId = isset($param['hotel']) ? $param['hotel'] : null;
        $userId = getUserId();

        $newsPushInfo = $newsPushLogic->getNewsList($hotelId, $userId);

        if (isset($newsPushInfo['newsList']) && empty($newsPushInfo['newsList'])) {
            $newsPushInfo = array();
        }

        if (empty($newsPushInfo)) {
            returnJson(403, 800, 'error');
        }

        // 过滤已读数据
        foreach ($newsPushInfo as $key => $newsPushItem) {
           if ($newsPushItem['newsType'] == 1) {
               unset($newsPushInfo[$key]);
           }
        }
        unset($newsPushItem);

        $newsPushInfo = array_values($newsPushInfo);
        // 显示前面30条
        $newsPushInfo = createPageInfo($newsPushInfo, 1, 30);

        returnJson(200, 200, $newsPushInfo['data']);
	}

    /**
     * 消息推送编辑
     * @modify wenhui 2017-03-16    创建
     */
    public function saveNews() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'newsPushId' => 'notnull,notempty,id',
            'newsPushRelateId' => 'notnull',
            'type' => 'notnull,notempty',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);


        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $newsPushRelateId = $param['newsPushRelateId'];
        $type = $param['type'];
        $newsPushId = $param['newsPushId'];
        $userId = getUserId();

        // 保存该用户信息全部已读 type = 3
        if ($type == 3) {
            $saveInfo = array(array('newsPushRelateId' =>'', 'type' => $type, 'newsPushId' => $newsPushId));
        } else {
            // 只根据 newsPushRelateId 修改
            $saveInfo = array(array('newsPushRelateId' =>$newsPushRelateId, 'type' => $type, 'newsPushId' => $newsPushId));
        }

        $newsPushInfo = $newsPushLogic->saveNews($saveInfo, $userId);
        if ($newsPushInfo) {
            returnJson(200, 200, array());
        }     
    }

    /**
     * 消息推送操作历史记录
     * @modify wenhui 2017-03-16    创建
     */
    public function newsHistory() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'newsPushId' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $newsId = $param['newsPushId'];

        $newsPushInfo = $newsPushLogic->newsHistory($newsId);

        returnJson(200, 200, array(
            'newsList' => $newsPushInfo,
        ));
        
    }

    /**
     * 设置声音
     * @modify wenhui 2017-03-16    创建
     */
    public function setVoice() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'type' => 'notnull,notempty',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $hotel = $param['hotel'];
        $type = $param['type'];

        $newsPushInfo = $newsPushLogic->setVoice($hotel,$type);

        returnJson(200, 200, array());
        
    }
     /**
     * 获取数据
     * @modify wenhui 2017-03-16    创建
     */
    public function getVoice() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $hotel = $param['hotel'];

        $newsPushInfo = $newsPushLogic->getVoice($hotel);

        if ($newsPushInfo != false) {
            returnJson(200, 200, array('voiceStatus' => $newsPushInfo['voicestatus']));
        }
    }

    /**
     * 获取未读信息数量
     * @modify wenhui 2017-03-16    创建
     */
    public function getHotelNewsCount() {

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $hotelId = $param['hotel'];
        $userId = getUserId();

        $AdminHotelModel = D('AdminHotel');
        $adminHotelInfo = $AdminHotelModel->getInfoByUserLeftHotelEntity($userId);
        $hotelIds = array_column($adminHotelInfo, 'hotelentity_id');

        // 获取数据
        $hotelNewsPushRelateModel = D('HotelNewspushRelate');
        $pushRelateInfo = $hotelNewsPushRelateModel->getNewsRelateByHotelAndUserId($hotelIds, $userId, 0);
        $countNews = empty(count($pushRelateInfo)) ? 0 : count($pushRelateInfo);
        // 推送数据web端
        $HotelEntityModel = D('HotelEntity');
        $HotelEntityInfo = $HotelEntityModel->getInfo($hotelId);
        $voiceStatus = $HotelEntityInfo['voicestatus'];
        // 提醒获取数据
        $hotelAlarmClockModel = D('HotelAlarmClock');
        $hotelAlarmClockInfo = $hotelAlarmClockModel->getRemindListByHotel($hotelIds, 1, 0);
        $remindCountNews = empty(count($hotelAlarmClockInfo)) ? 0 : count($hotelAlarmClockInfo);
        // 获取查房消息数据
        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $roomCheckInfo = $newsPushLogic->getCheckInfo($hotelId, null, 1, 30, 1, $userId);
        $checkCount = empty(count($roomCheckInfo['roomCheck'])) ? 0 : count($roomCheckInfo['roomCheck']);

        foreach ($pushRelateInfo as $key => &$value) {
            $value['createtime'] = strtotime($value['createtime']);
        }
        unset($value);
        foreach ($hotelAlarmClockInfo as $key => &$value) {
            $value['trueremindtime'] = strtotime($value['trueremindtime']);
        }
        unset($value);

        /**
         *  排序
         *  排序方式 推荐客栈按点击量  有城市搜索 按最近距离
         *  SORT_ASC - 默认，按升序排列。(A-Z)
         *  SORT_DESC - 按降序排列。(Z-A)
         *  随后可以指定排序的类型：
         *  SORT_REGULAR - 默认。将每一项按常规顺序排列。
         *  SORT_NUMERIC - 将每一项按数字顺序排列。
         *  SORT_STRING - 将每一项按字母顺序排列
         */
        $pushRelateInfo = my_sort($pushRelateInfo, 'createtime', SORT_DESC, SORT_NUMERIC);
        $hotelAlarmClockInfo = my_sort($hotelAlarmClockInfo, 'trueremindtime', SORT_DESC, SORT_NUMERIC);
        // 判断哪种消息最新
        if ($pushRelateInfo[0]['createtime'] >= $hotelAlarmClockInfo[0]['trueremindtime']) {
            $preNews = 1;
        } else {
            $preNews = 0;
        }

        returnJson(200, 200, array('newsCount' => $countNews, 'voiceStatus' => $voiceStatus, 'remindCountNews' => $remindCountNews, 'preNews' => $preNews, 'checkCount' => $checkCount));
    }

    /**
     * 掌柜说推送
     * @modify wenhui 2017-03-24 创建
     */
    public function pushManage() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $hotelId = $param['hotel'];
        $specialId = $param['specialId'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }
        // 推送
        $pushResult = $hotelBookLogic->pushManage($hotelId, $specialId, null, true);

        returnJson(200, 200);
    }

    /**
     * 删除查房消息
     * @modify wenhui 2018-02-127     创建
     */
    public function deleteRoomCheck() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty',
            'checkId' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $hotelId = $param['hotel'];
        $checkId = $param['checkId'];

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        try {
            $deleteResult = $newsPushLogic->deleteCheck($checkId);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 获取查房消息
     * @modify wenhui 2018-02-127     创建
     */
    public function getRoomCheck() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $hotelId = $param['hotel'];
        $userId = getUserId();
        // 获取数据
        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $roomCheckInfo = $newsPushLogic->getCheckInfo($hotelId, null, 1, 30, 1, $userId);

        // 处理数据
        foreach ($roomCheckInfo['roomCheck'] as $key => &$roomCheck) {
            unset($roomCheck['name']);
            unset($roomCheck['endTime']);
            unset($roomCheck['typeName']);
            unset($roomCheck['orderInfoId']);
            unset($roomCheck['orderRoomId']);
            unset($roomCheck['roomId']);
            unset($roomCheck['createStamp']);
            unset($roomCheck['createTime']);
        }

        returnJson(200, 200, $roomCheckInfo);
    }

    /**
     * 设置消息状态
     * @modify wenhui 2018-02-127     创建
     */
    public function setRoomCheckType() {
        $param = I('post.');

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'checkId' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $checkId = $param['checkId'];
        $hotelRoomCheckModel = D('HotelRoomCheck');
        $saveResult = $hotelRoomCheckModel->saveInfo($checkId, null, null, 2);

        returnJson(200, 200);
    }
}